/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddEventDialog.java
 *
 * Created on Apr 2, 2011, 3:48:20 PM
 */
package ohtu;

import java.text.ParseException;
import java.util.Calendar;
import javax.swing.JOptionPane;

/**
 *
 * @author johannes
 */
public class AddEventDialog extends javax.swing.JDialog {

	private Controller c;
	private Course course;
	private Event selectedEvent;

	/** Creates new form AddEventDialog */
	public AddEventDialog(java.awt.Frame parent, Controller c, Course course, Calendar selectedTime, Event selectedEvent) {
		super(parent, true);
		this.c = c;
		this.course = course;
		initComponents();

		this.selectedEvent = selectedEvent;
		if (selectedEvent == null) {
			startingField.setText(OhtuView.dateFormat.format(selectedTime.getTime()));
			startingTimeField.setText(OhtuView.timeFormat.format(selectedTime.getTime()));
		} else {
			startingField.setText(OhtuView.dateFormat.format(selectedEvent.getStartDate().getTime()));
			startingTimeField.setText(OhtuView.timeFormat.format(selectedEvent.getStartDate().getTime()));
			descriptionField.setText(selectedEvent.getName());
			locationField.setText(selectedEvent.getLocation());
			informationField.setText(selectedEvent.getInformation());
		}
		setVisible(true);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                cancelButton = new javax.swing.JButton();
                okButton = new javax.swing.JButton();
                startingLabel = new javax.swing.JLabel();
                durationLabel = new javax.swing.JLabel();
                durationComboBox = new javax.swing.JComboBox();
                startingField = new javax.swing.JTextField();
                recurringCheckbox = new javax.swing.JCheckBox();
                descriptionLabel = new javax.swing.JLabel();
                locationLabel = new javax.swing.JLabel();
                informationLabel = new javax.swing.JLabel();
                informationField = new javax.swing.JTextField();
                locationField = new javax.swing.JTextField();
                descriptionField = new javax.swing.JTextField();
                startingTimeLabel = new javax.swing.JLabel();
                startingTimeField = new javax.swing.JTextField();

                setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
                setName("Form"); // NOI18N

                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ohtu.OhtuApp.class).getContext().getResourceMap(AddEventDialog.class);
                cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
                cancelButton.setName("cancelButton"); // NOI18N
                cancelButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                cancelButtonActionPerformed(evt);
                        }
                });

                okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
                okButton.setName("okButton"); // NOI18N
                okButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                okButtonActionPerformed(evt);
                        }
                });

                startingLabel.setText(resourceMap.getString("startingLabel.text")); // NOI18N
                startingLabel.setName("startingLabel"); // NOI18N

                durationLabel.setText(resourceMap.getString("durationLabel.text")); // NOI18N
                durationLabel.setName("durationLabel"); // NOI18N

                durationComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "4", "6", "8", "10" }));
                durationComboBox.setName("durationComboBox"); // NOI18N

                startingField.setText(resourceMap.getString("startingField.text")); // NOI18N
                startingField.setName("startingField"); // NOI18N
                startingField.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                startingFieldActionPerformed(evt);
                        }
                });

                recurringCheckbox.setText(resourceMap.getString("recurringCheckbox.text")); // NOI18N
                recurringCheckbox.setName("recurringCheckbox"); // NOI18N

                descriptionLabel.setText(resourceMap.getString("descriptionLabel.text")); // NOI18N
                descriptionLabel.setName("descriptionLabel"); // NOI18N

                locationLabel.setText(resourceMap.getString("locationLabel.text")); // NOI18N
                locationLabel.setName("locationLabel"); // NOI18N

                informationLabel.setText(resourceMap.getString("informationLabel.text")); // NOI18N
                informationLabel.setName("informationLabel"); // NOI18N

                informationField.setText(resourceMap.getString("informationField.text")); // NOI18N
                informationField.setName("informationField"); // NOI18N

                locationField.setText(resourceMap.getString("locationField.text")); // NOI18N
                locationField.setName("locationField"); // NOI18N

                descriptionField.setText(resourceMap.getString("descriptionField.text")); // NOI18N
                descriptionField.setName("descriptionField"); // NOI18N

                startingTimeLabel.setText(resourceMap.getString("startingTimeLabel.text")); // NOI18N
                startingTimeLabel.setName("startingTimeLabel"); // NOI18N

                startingTimeField.setText(resourceMap.getString("startingTimeField.text")); // NOI18N
                startingTimeField.setName("startingTimeField"); // NOI18N

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addComponent(durationLabel)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(durationComboBox, 0, 89, Short.MAX_VALUE))
                                                .addComponent(recurringCheckbox))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(startingLabel)
                                                        .addComponent(startingTimeLabel))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(startingTimeField)
                                                        .addComponent(startingField))))
                                .addGap(58, 58, 58)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(okButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(cancelButton))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(informationLabel)
                                                        .addComponent(locationLabel)
                                                        .addComponent(descriptionLabel))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(locationField, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                                                        .addComponent(informationField, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                                                        .addComponent(descriptionField, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))))
                                .addContainerGap())
                );

                layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(startingLabel)
                                        .addComponent(startingField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(descriptionLabel)
                                        .addComponent(descriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(locationLabel)
                                        .addComponent(locationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(startingTimeLabel)
                                        .addComponent(startingTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(informationLabel)
                                                        .addComponent(informationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(cancelButton)
                                                        .addComponent(okButton)))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(durationLabel)
                                                        .addComponent(durationComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(recurringCheckbox)))
                                .addContainerGap())
                );

                pack();
        }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

	    Calendar calendar = Calendar.getInstance();
	    try {
		    calendar.setTime(OhtuView.dateFormat.parse(startingField.getText()));
		    Calendar timeOfDay = Calendar.getInstance();
		    timeOfDay.setTime(OhtuView.timeFormat.parse(startingTimeField.getText()));
		    calendar.set(Calendar.HOUR_OF_DAY, timeOfDay.get(Calendar.HOUR_OF_DAY));
		    calendar.set(Calendar.MINUTE, timeOfDay.get(Calendar.MINUTE));
	    } catch (ParseException ex) {
		    JOptionPane.showMessageDialog(this, "YOU FAIL --- LAITA KUNNOLLINEN DATE", "EPIC FAIL", JOptionPane.ERROR_MESSAGE);
		    return;
	    }
	    c.removeEventFromContainer(selectedEvent);
	    c.addEventToContainer(descriptionField.getText(), calendar, Integer.parseInt((String) durationComboBox.getSelectedItem()), recurringCheckbox.isSelected(), informationField.getText(), locationField.getText(), course);

	    this.dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
	    this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void startingFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startingFieldActionPerformed
	    // TODO add your handling code here:
    }//GEN-LAST:event_startingFieldActionPerformed
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton cancelButton;
        private javax.swing.JTextField descriptionField;
        private javax.swing.JLabel descriptionLabel;
        private javax.swing.JComboBox durationComboBox;
        private javax.swing.JLabel durationLabel;
        private javax.swing.JTextField informationField;
        private javax.swing.JLabel informationLabel;
        private javax.swing.JTextField locationField;
        private javax.swing.JLabel locationLabel;
        private javax.swing.JButton okButton;
        private javax.swing.JCheckBox recurringCheckbox;
        private javax.swing.JTextField startingField;
        private javax.swing.JLabel startingLabel;
        private javax.swing.JTextField startingTimeField;
        private javax.swing.JLabel startingTimeLabel;
        // End of variables declaration//GEN-END:variables
}
